﻿Ext.define('ProjectPhaseTreeNode', {
    extend: 'Ext.data.Model',
    fields: [
       "indexno",
       "isEnabled",
        "isFinsh",
        "leaf",
        "note",
        "parentId",
        {
            name: 'planFinishDate', type: 'date',
            dateFormat: 'Y-m-d\\TH:i:s'
        }
    ]
});

Ext.define('app.prm.detail.Phase', {
    extend: 'Ext.tree.Panel',
    border: false,
    useArrows: true,
    rootVisible: false,
    projectId: 0,
    title: '阶段进展',
    forceFit: true,
    tools: [
        {
            type: 'refresh',
            itemId: "refresh",
            handler: function () {
                this.ownerCt.ownerCt.store.reload();
            } //some logic inside handler
        }
    ],
    reference: 'projectphase',
    initComponent: function () {
        var me = this;

        



        me.store = new Ext.data.TreeStore({
            root: { expanded: true },
            model: 'ProjectPhaseTreeNode',
            autoLoad: true,
            nodeParam: 'parentId',
            // 根节点的参数值是0
            defaultRootId: "0",
            proxy: {
                type: 'ajax',
                reader: {
                    type: 'json',
                    rootProperty: function (node) {
                        return node.result || node.children;
                    }
                },
                url: '/api/services/app/project/GetProjectPhase?projectId=' + me.projectId
            }
        });
        //保存按钮，提到上层界面了；
        // me.tbar = ["->", { text: '保存', icon: '/Content/images/page_save.png', handler: function () { me.savePhase(); } }];
        me.callParent();
    },
    plugins: [
        Ext.create('Ext.grid.plugin.CellEditing', {
            clicksToEdit: 1
        })
    ],
    columns: [{
        xtype: 'treecolumn',
        text: '阶段',
        width: 200,
        sortable: false,
        dataIndex: 'text'
    },
    {
        maxWidth: 100,
        sortable: false,
        header: '排序',
        dataIndex: 'indexno',
        editor: {}
    },
    {
        maxWidth: 100,
        minWidth: 100,
        xtype:'datecolumn',
        sortable: false,
        header: '预计完成',
        dataIndex: 'planFinishDate',
        format:'Y-m-d',
        //render:function(val){}
        editor: {
            xtype: 'datefield',
            format: 'Y-m-d'
        }
    },
    {
        xtype: 'checkcolumn',
        maxWidth: 100,
        sortable: false,
        header: '是否完成',
        dataIndex: 'isFinsh'
    },
    {
        text: '描述',
        width: 200,
        dataIndex: 'note',
        sortable: false,
        editor: {}
    }],
    root: { id: '0', text: '' },
    listeners: {
        afterrender: function (tree) { tree.expandAll(); }
    },
    savePhase: function (taskNote) {
        var me = this;
        var arry = [];
        var updates = me.store.getUpdatedRecords();
        for (var i = 0; i < updates.length; i++) {
            var record = updates[i];
            arry.push({
                id: record.data.id, isFinsh: record.data.isFinsh, note: record.data.note,
                indexno: record.data.indexno,
                planFinishDate: Ext.Date.format(record.data.planFinishDate, 'Y-m-d')
            });
        }
        if (arry.length > 0) {
            App.AjaxPost('/api/services/app/project/SetProjectPhase', arry, function (data) {
                me.store.load();
            });
        }
        Ext.Msg.alert('系统提示', "保存成功");
        return false;
    }
})